package longrise.phone.com.bjjt_henan.tinker;

import android.annotation.TargetApi;
import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.support.multidex.MultiDex;

import com.baidu.mapapi.SDKInitializer;
import com.baidu.mobstat.StatService;
import com.longrise.LEAP.Base.Objects.EntityBean;
import com.longrise.bjjt.BaseApplication;
import com.longrise.bjjt.util.PrefUtils;
import com.meituan.android.walle.WalleChannelReader;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.tencent.tinker.lib.tinker.Tinker;
import com.tencent.tinker.loader.app.DefaultApplicationLike;
import com.umeng.socialize.PlatformConfig;
import com.umeng.socialize.UMShareAPI;

import longrise.phone.com.bjjt_henan.tinker.util.TinkerManager;

/**
 * Application的代理实现类
 */
@SuppressWarnings("unused")
public class TinkerApplicationLike extends DefaultApplicationLike {

    private static final String TAG = "TinkerApplicationLike";

    private BaseApplication appData;//数据实体类

    public TinkerApplicationLike(Application application, int tinkerFlags, boolean tinkerLoadVerifyFlag,
                                 long applicationStartElapsedTime, long applicationStartMillisTime, Intent tinkerResultIntent) {
        super(application, tinkerFlags, tinkerLoadVerifyFlag, applicationStartElapsedTime, applicationStartMillisTime, tinkerResultIntent);
    }

    @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
    @Override
    public void onBaseContextAttached(Context base) {
        super.onBaseContextAttached(base);
        MultiDex.install(base);
        TinkerManager.setTinkerApplicationLike(this);
        TinkerManager.setUpgradeRetryEnable(true);
        TinkerManager.installTinker(this);
        Tinker tinker = Tinker.with(getApplication());
    }

    @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
    public void registerActivityLifecycleCallbacks(Application.ActivityLifecycleCallbacks callback) {
        getApplication().registerActivityLifecycleCallbacks(callback);
    }

    /**
     * Tinker通过反射机制执行，等同于执行Application中的onCreate方法
     */
    @Override
    public void onCreate() {
        appData = new BaseApplication();
        appData.setmContext(getApplication());
        // 获取登录信息
        String loginInfoStr = PrefUtils.getString(getApplication(), "loginInfo", null);
        if (null != null && !"".equals(loginInfoStr)) {
            EntityBean loginInfo = (EntityBean) com.longrise.LEAP.Base.IO.JSONSerializer.getInstance()
                    .DeSerialize(loginInfoStr, EntityBean.class);
            if (null != loginInfo) {
                EntityBean userInfo = loginInfo.getBean("userinfo");
                appData.setToken(userInfo.getString("token", null));
            }
        }
        //百度地图
        SDKInitializer.initialize(getApplication());
        //初始化imageloader
        ImageLoader.getInstance().init(ImageLoaderConfiguration.createDefault(getApplication()));
        //百度统计自动埋点
        StatService.autoTrace(getApplication(), true);
        //动态设置渠道名
        StatService.setAppChannel(getApplication(), WalleChannelReader.getChannel(getApplication()), true);
        UMShareAPI.get(getApplication());
    }

    //各个平台的分享配置，建议放在全局Application或者程序入口
    {
        //微信 appid appsecret
        PlatformConfig.setWeixin("wx854b0d531e67d094", "fbddb098a228bd752aee54236d3f1a5a");
        //QQ和Qzone appid appkey
        PlatformConfig.setQQZone("1105798446", "VMjaHACQOYVwaPtr");
    }

    public BaseApplication getDataApp() {
        return appData;
    }

    //相关参数
    private int mainLevel;//主界面Form的唯一标识level

    private int histroyLevel;//历史界面Form的唯一标识level

    public void setMainLevel(int mainLevel) {
        this.mainLevel = mainLevel;
    }

    public int getMainLevel() {
        return mainLevel;
    }

    public void setHistroyLevel(int histroyLevel) {
        this.histroyLevel = histroyLevel;
    }

    public int getHistroyLevel() {
        return histroyLevel;
    }
}
